package com.crk.yiyuanguahao.service.impl;



import com.crk.yiyuanguahao.dao.RoleAuthDao;
import com.crk.yiyuanguahao.dao.RoleDao;
import com.crk.yiyuanguahao.dao.UserDao;
import com.crk.yiyuanguahao.entity.Role;
import com.crk.yiyuanguahao.entity.RoleAuth;
import com.crk.yiyuanguahao.entity.User;
import com.crk.yiyuanguahao.entity.menu.SysFunctionTreeDto;
import com.crk.yiyuanguahao.service.RoleAuthService;
import com.crk.yiyuanguahao.util.ResponseResult;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;

/**
 * @Author: 程荣凯
 * @Date: 2019/3/5 9:56
 */
@Service
public class RoleAuthServiceImpl implements RoleAuthService {
    @Autowired
    RoleAuthDao roleAuthDao;
    @Autowired
    UserDao userDao;
    @Autowired
    RoleDao roleDao;

    /**
     * 保存角色用户关联
     *
     * @param roleAuth
     * @return
     */
    @Override
    public ResponseResult saveRoleAuth(RoleAuth roleAuth) {
        try {
            roleAuth.setStatus((short)1);
            roleAuth.setCreateTime(new Date());
            roleAuth.setUpdateTime(new Date());
            User userInfo = (User) SecurityUtils.getSubject().getPrincipal();
            roleAuth.setCreator(userInfo.getId());
            roleAuth.setUpdator(userInfo.getId());
            int insert = roleAuthDao.insert(roleAuth);
            if (insert > 0){
                return ResponseResult.success(roleAuth);
            }else{
                return ResponseResult.fail();
            }

        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.fail();
        }
    }

    /**
     * 根据用户ID查询角色列表
     *
     * @param userId
     * @return
     */
    @Override
    public ResponseResult queryRoleListTreeByUser(int userId) {
        List<Role> roleList = userDao.getRoleList(userId);
        List<SysFunctionTreeDto> childrenTree = getChildrenTree(roleList);
        return ResponseResult.success(childrenTree);
    }

    private List<SysFunctionTreeDto> getChildrenTree(List<Role> roleList){
        List<Role> roles = roleDao.selectAll();
        if (null == roles || roles.size() == 0){
            return null;
        }else {
            HashSet<Integer> set = new HashSet<Integer>();
            if (null != roleList && roleList.size() >0){
                //判断是否被选中
                for (Role role : roleList) {
                    set.add(role.getId());
                }
            }
            List<SysFunctionTreeDto> functionList = new ArrayList<>();
            for (Role role : roles) {
                SysFunctionTreeDto sysFunctionTreeDto = new SysFunctionTreeDto();
                sysFunctionTreeDto.setKey(role.getId());
                sysFunctionTreeDto.setLabel(role.getRoleName());
                sysFunctionTreeDto.setSpread(false);
                sysFunctionTreeDto.setChecked(false);
                if (set.contains(role.getId())){
                    sysFunctionTreeDto.setChecked(true);
                }
                functionList.add(sysFunctionTreeDto);
            }
            return functionList;
        }

    }
}
